package com.cybertron.ironhide.manage.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cybertron.ironhide.manage.common.Constants;
import com.cybertron.ironhide.manage.common.base.Pager;
import com.cybertron.ironhide.manage.domain.Article;
import com.cybertron.ironhide.manage.persistence.ArticleMapper;
import com.cybertron.ironhide.manage.service.ArticleService;
import com.cybertron.ironhide.manage.util.CommonUtil;
import com.cybertron.ironhide.manage.util.PageUtil;

@Service
public class ArticleServiceImpl implements ArticleService {

	@Resource
	private ArticleMapper articleMapper;

	@Resource
	private PageUtil pageUtil;

	@Override
	public List<Article> selectBySelective(Article article, Pager pager) {
		return articleMapper.selectBySelective(article, pager);
	}

	@Override
	public void insertSelective(Article record) {
		record.setId(CommonUtil.getUUID());
		record.setCreateDate(new Date());
		record.setModifyDate(new Date());
		articleMapper.insert(record);
		createHtml();
	}

	@Override
	public Article selectByPrimaryKey(String id) {
		return articleMapper.selectByPrimaryKey(id);
	}

	@Override
	public void updateByPrimaryKeySelective(Article record) {
		record.setModifyDate(new Date());
		articleMapper.updateByPrimaryKeySelective(record);
		createHtml();
	}

	@Override
	public void deleteByPrimaryKey(String id) {
		articleMapper.deleteByPrimaryKey(id);
	}

	@Override
	public void deleteByPrimarykeys(String[] articleIds) {
		articleMapper.deleteByPrimaryKeys(articleIds);
		createHtml();
	}

	@Override
	public List<Article> selectTop(Integer type, Integer topNum) {
		return articleMapper.selectTop(type, topNum);
	}

	private void createHtml() {
		List<Article> topArticles = articleMapper.selectTop(
				Constants.TOP_ARTICLE_TYPE, Constants.TOP_ARTICLE_COUNT - 1);

		List<Article> botArticles = articleMapper.selectTop(
				Constants.BOT_ARTICLE_TYPE, Constants.BOT_ARTICLE_COUNT);
		createHtml(topArticles, botArticles, new Pager());
		Article record = new Article();
		record.setType(Constants.BOT_ARTICLE_TYPE);
		List<Article> siteMapArticles = articleMapper.selectBySelective(record);
		if (siteMapArticles != null && siteMapArticles.size() != 0) {
			pageUtil.createArticleHTML(siteMapArticles, topArticles,
					botArticles, null);
		}
	}

	private void createHtml(List<Article> topArticles,
			List<Article> botArticles, Pager pager) {
		Article record = new Article();
		record.setType(Constants.TOP_ARTICLE_TYPE);
		List<Article> articles = articleMapper.selectBySelective(record, pager);
		if (articles != null && articles.size() != 0) {
			pageUtil.createArticleHTML(articles, topArticles, botArticles,
					pager);
		} else {
			return;
		}
		createHtml(topArticles, botArticles, new Pager(pager.getPageNum() + 1,
				pager.getNumPerPage()));
	}

	@Override
	public boolean checkTitle(String title) {
		Article article = articleMapper.selectByTitle(title);
		if (article != null) {
			return true;
		}
		return false;
	}

	
}
